package com.xs.demo.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xs.demo.entity.Userinfo;
import com.xs.demo.util.Constants;

public class LoginService extends BaseService{
	
	private static Log log = LogFactory.getLog(LoginService.class);
	UserService userService;

	public String webLogin(String username, String password,HttpServletRequest request) {
		List<Userinfo> userlist = userService.list(username,password);
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == userlist || userlist.size() == 0){
			map = new HashMap<String, Object>();
			map.put("msg","用户名或者密码错误");
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
			String s = gson.toJson(map);
			return s;
		}
		Userinfo userinfo = userlist.get(0);
		userinfo.setBirthday(new Date());
		super.update(userinfo);
		
		request.getSession().setAttribute(Constants.LOGIN_INFO, userinfo);
		
		map = new HashMap<String, Object>();
		map.put("success", true);
		map.put("msg", "登陆成功");
		map.put("name", userinfo.getName());
		map.put("user", userlist.get(0));
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		String s = gson.toJson(map);
		return s;
	}
	/*-------set get----------*/
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
